<?php

namespace app\common\validate;

use app\common\validate\Base;

class Consult extends Base
{

    protected $rule = [
        'pid|工单主题ID' => 'require|integer|checkConsultPid:pid',
        'title|工单标题' => 'require|min:2',
        'content|工单内容' => 'require|min:2',
        'post_time|提交时间' => 'checkConsultSame:post_time',
        'captcha|验证码'=>'require|captcha'
    ];

    protected $message = [

    ];

    protected $scene = [
        // 前台提交工单
        'home_submit' => ['title','content','post_time','captcha'],
        // 前台回复工单
        'home_reply' => ['title','content','pid'],
    ];

    protected function checkConsultPid($v,$rule,$data){
        if($v==0){
            return true;
        }
        $model = new \app\common\model\Consult;
        $row = $model->find($v);
        if(!$row||$row['isover']){
            $this->message[$rule.'.checkConsultPid'] = '提交失败，该工单已经关闭或不存在';
            return false;
        }
        return true;
    }

    protected function checkConsultSame($v,$rule,$data){

        $model = new \app\common\model\Consult;
        if($model->where([$rule=>$v])->find()){
            $this->message[$rule.'.checkConsultSame'] = '请勿重复提交工单';
            return false;
        }else{
            return true;
        }
    }

}